package initialize

import (
	"github.com/olivere/elastic/v7"
	"go.uber.org/zap"
	"go_es/server/global"
)

func Elastic()  {
	elasticCfg := global.GVA_CONFIG.Elastic
	//创建ES client用于后续操作ES
	client, err := elastic.NewClient(
		// 设置ES服务地址，支持多个地址
		elastic.SetURL(elasticCfg.Addr),
		elastic.SetSniff(false),
		// 设置基于http base auth验证的账号和密码
		elastic.SetBasicAuth(elasticCfg.Username, elasticCfg.Password))
	if err != nil {
		global.GVA_LOG.Error("elastic connect failed, error:", zap.Error(err))
	} else {
		global.GVA_LOG.Info("elastic connect success")
		global.GVA_ES = client
	}
}